/*
 * Copyright 2018 JessYan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package me.jessyan.armscomponent.app.mvp.ui.activity;

import static me.jessyan.armscomponent.app.mvp.ui.util.BlueToothUtil.REQUEST_BLUETOOTH_SCAN_RESULT;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Switch;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import butterknife.BindView;
import butterknife.OnClick;
import me.jessyan.armscomponent.app.R;
import me.jessyan.armscomponent.app.mvp.ui.model.DeviceInformation;
import me.jessyan.armscomponent.app.mvp.ui.util.BlueToothUtil;
import me.jessyan.armscomponent.commonsdk.core.RouterHub;

/**
 * ================================================
 * 宿主 App 的主页
 *
 * @see <a href="https://github.com/JessYanCoding/ArmsComponent/wiki">ArmsComponent wiki 官方文档</a>
 * Created by JessYan on 19/04/2018 16:10
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * ================================================
 */
@Route(path = RouterHub.APP_CONTROLACTIVITY)
public class ControlActivity extends BaseActivity {
    @BindView(R.id.switch_way)
    Switch mSwitch;
    @BindView(R.id.tv_device_name)
    TextView mDeviceName;


    private int way;// 0:蓝牙 1：wifi
    private DeviceInformation mDeviceInfo;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {

    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_control;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
       way = getIntent().getIntExtra("way",0);
       String mSwitchName = "";
       switch (way){
           case 0://蓝牙开关
               mSwitchName = "蓝牙开关";
               break;
           case 1://Wi-Fi开关
               mSwitchName = "WIFI开关";
               break;

       }
       mSwitch.setText(mSwitchName);
       mSwitch.setChecked(BlueToothUtil.checkBlueToothIsEnable());
    }

    @OnClick({R.id.iv_back,R.id.btn_sensor,R.id.btn_model,R.id.tv_device_name,R.id.switch_way,R.id.btn_connect})
    public void onClick(View v){
        switch (v.getId()){
            case R.id.iv_back:
                onBackPressed();
                break;
            case R.id.btn_sensor:
                ArmsUtils.startActivity(SensorActivity.class);
                break;
            case R.id.btn_model:
                ArmsUtils.startActivity(ModelActivity.class);
                break;
            case R.id.switch_way:
                mSwitch.setChecked(BlueToothUtil.switchBlueToothIsEnable());
                break;
            case R.id.tv_device_name:
                Intent intent = new Intent(this,ScanDeviceActivity.class);
                intent.putExtra("way",way);
                this.startActivityForResult(intent,REQUEST_BLUETOOTH_SCAN_RESULT);
                break;
            case R.id.btn_connect:
                BlueToothUtil.getInstance(this).connectDevice(mDeviceInfo.getDeviceAddress());
                break;
            case R.id.btn_1:
                BlueToothUtil.getInstance(this).sendMessage("model1");
                break;
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        BlueToothUtil.getInstance(this).destroyConnected();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode){
            case REQUEST_BLUETOOTH_SCAN_RESULT:
                if(data != null){
                   String name = data.getStringExtra("name");
                   String address = data.getStringExtra("address");
                   mDeviceInfo = new DeviceInformation(name,address);
                   mDeviceName.setText(name);
                }
                break;
        }

    }
}
